// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.globalObjects;

import se.krka.kahlua.vm.KahluaTable;
import se.krka.kahlua.vm.KahluaTableIterator;
import zombie.Lua.LuaManager;
import zombie.characters.IsoPlayer;
import zombie.core.BoxedStaticValues;

public final class CGlobalObjectSystem extends GlobalObjectSystem {
    public CGlobalObjectSystem(String name) {
        super(name);
    }

    @Override
    protected GlobalObject makeObject(int int0, int int1, int int2) {
        return new CGlobalObject(this, int0, int1, int2);
    }

    public void sendCommand(String string, IsoPlayer isoPlayer, KahluaTable kahluaTable) {
        CGlobalObjectNetwork.sendClientCommand(isoPlayer, this.name, string, kahluaTable);
    }

    public void receiveServerCommand(String string, KahluaTable kahluaTable) {
        Object object = this.modData.rawget("OnServerCommand");
        if (object == null) {
            throw new IllegalStateException("OnServerCommand method undefined for system '" + this.name + "'");
        } else {
            LuaManager.caller.pcallvoid(LuaManager.thread, object, this.modData, string, kahluaTable);
        }
    }

    public void receiveNewLuaObjectAt(int int2, int int1, int int0, KahluaTable kahluaTable) {
        Object object = this.modData.rawget("newLuaObjectAt");
        if (object == null) {
            throw new IllegalStateException("newLuaObjectAt method undefined for system '" + this.name + "'");
        } else {
            LuaManager.caller
                .pcall(
                    LuaManager.thread,
                    object,
                    new Object[]{
                        this.modData,
                        BoxedStaticValues.toDouble((double)int2),
                        BoxedStaticValues.toDouble((double)int1),
                        BoxedStaticValues.toDouble((double)int0)
                    }
                );
            GlobalObject globalObject = this.getObjectAt(int2, int1, int0);
            if (globalObject != null) {
                KahluaTableIterator kahluaTableIterator = kahluaTable.iterator();

                while (kahluaTableIterator.advance()) {
                    globalObject.getModData().rawset(kahluaTableIterator.getKey(), kahluaTableIterator.getValue());
                }
            }
        }
    }

    public void receiveRemoveLuaObjectAt(int x, int y, int z) {
        Object object = this.modData.rawget("removeLuaObjectAt");
        if (object == null) {
            throw new IllegalStateException("removeLuaObjectAt method undefined for system '" + this.name + "'");
        } else {
            LuaManager.caller
                .pcall(
                    LuaManager.thread,
                    object,
                    new Object[]{
                        this.modData, BoxedStaticValues.toDouble((double)x), BoxedStaticValues.toDouble((double)y), BoxedStaticValues.toDouble((double)z)
                    }
                );
        }
    }

    public void receiveUpdateLuaObjectAt(int int0, int int1, int int2, KahluaTable kahluaTable) {
        GlobalObject globalObject = this.getObjectAt(int0, int1, int2);
        if (globalObject != null) {
            KahluaTableIterator kahluaTableIterator = kahluaTable.iterator();

            while (kahluaTableIterator.advance()) {
                globalObject.getModData().rawset(kahluaTableIterator.getKey(), kahluaTableIterator.getValue());
            }

            Object object = this.modData.rawget("OnLuaObjectUpdated");
            if (object == null) {
                throw new IllegalStateException("OnLuaObjectUpdated method undefined for system '" + this.name + "'");
            } else {
                LuaManager.caller.pcall(LuaManager.thread, object, new Object[]{this.modData, globalObject.getModData()});
            }
        }
    }

    @Override
    public void Reset() {
        super.Reset();
        this.modData.wipe();
    }
}
